package org.gy.T12.t2;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test3Method {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
//1、反射第⼀步：先获取到Class对象
        Class c = Cat.class;
//2、获取类中的全部成员⽅法
        Method[] methods = c.getDeclaredMethods();
//3、遍历这个数组中的每⼀个⽅法对象
        for(Method method : methods){
            System.out.println(method.getName()+"-->"+method.getParameterCount()+"-->"+method.getReturnType());
        }

        //4、获取private修饰的run⽅法，得到Method对象
        Method run = c.getDeclaredMethod("run");
//执⾏run⽅法,在执⾏前需要取消权限检查
        Cat cat = new Cat();
        run.setAccessible(true);
        Object rs1 = run.invoke(cat);
        System.out.println(rs1);
//5、获取private 修饰的eat(String name)⽅法，得到Method对象
        Method eat = c.getDeclaredMethod("eat",String.class);
        eat.setAccessible(true);
        Object rs2 = eat.invoke(cat,"⻥⼉");
        System.out.println(rs2);
    }
}
